<template>
  <el-dialog
    title="分配角色"
    :visible="toRoleData.showRoleDialog"
    @close="btnClost"
  >
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <!-- 分配角色 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>

    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" style="width: 100px" @click="btnOK">确定</el-button>
        <el-button style="width: 100px" @click="btnClost">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { toAssignRoles, getUserDetailById } from '@/api/employees'
export default {
  props: {
    toRoleData: {
      type: Object,
      // 默认值
      default() {
        return {
          showRoleDialog: {
            type: Boolean,
            default: false
          },
          // 用户的id 用来查询当前用户的角色信息
          userId: {
            type: String,
            default: null
          }
        }
      }
    }
  },
  data() {
    return {
      list: [], // 角色列表
      roleIds: []
    }
  },
  created() {
    this.toGetRoleList()
  },
  methods: {
    // 获取该员工的基本信息
    async toGetUserDetailById(id) {
      const { roleIds } = await getUserDetailById(id)
      this.roleIds = roleIds
    },
    //  获取所有角色
    async toGetRoleList() {
      const { rows } = await getRoleList()
      this.list = rows
    },
    btnClost() {
      // 清除数据
      this.roleIds = []
      this.$parent.toRoleData.showRoleDialog = false
    },
    async btnOK() {
      const data = { id: this.toRoleData.userId, roleIds: this.roleIds }
      await toAssignRoles(data)
      this.$message.success('更新成功')
      this.$parent.toRoleData.showRoleDialog = false
    }

  }
}
</script>

<style lang="less" scoped>
</style>
